import time
from django.shortcuts import render
from django.views import View
# Create your views here.
# from fdfs_client.client import Fdfs_client
# client = Fdfs_client('utils/fastdfs/client.conf')
# client.upload_by_filename('/home/ubuntu/Desktop/1c270c04e1deae8c84646cde19af6fe7.jpg')
from utils.goods import get_categories, get_goods_specs
from apps.contents.models import ContentCategory


class IndexView(View):
    def get(self,request):
        categories = get_categories()
        contents = {}
        content_categories = ContentCategory.objects.all()
        for cat in content_categories:
            contents[cat.key] = cat.content_set.filter(status=True).order_by('sequence')
        context = {
            'categories':categories,
            'contents':contents
        }

        return render(request,'index.html',context)


from apps.goods.models import GoodsCategory
from utils.goods import get_breadcrumb
from apps.goods.models import SKU


class ListView(View):

    def get(self,request,category_id):
        ordering = request.GET.get('ordering')
        page_size = request.GET.get('page_size')
        page = request.GET.get('page')
        try:
            category = GoodsCategory.objects.get(id = category_id)
        except GoodsCategory.DoesNotExist:
            return JsonResponse({'code':400,'errmsg':'参数缺失'})
        breadcrumb=get_breadcrumb(category)
        skus = SKU.objects.filter(category=category,is_launched=True).order_by(ordering)
        from django.core.paginator import Paginator
        paginator = Paginator(skus,per_page=page_size)
        page_skus=paginator.page(page)

        sku_list=[]
        for sku in page_skus:
            sku_list.append({
                'id':sku.id,
                'name':sku.name,
                'price':sku.price,
                'default_image_url':sku.default_image.url

            })
        total_num = paginator.num_pages

        return JsonResponse({'code': 0,
                             'errmsg': 'ok',
                             'list': sku_list,
                             'count': total_num,
                             'breadcrumb': breadcrumb})


from haystack.views import SearchView
from django.http import JsonResponse


class SKUSearchView(SearchView):
    def create_response(self):
        context = self.get_context()
        sku_list=[]
        for sku in context['page'].object_list:
            sku_list.append({
                'id': sku.object.id,
                'name': sku.object.name,
                'price': sku.object.price,
                'default_image_url': sku.object.default_image.url,
                'searchkey': context.get('query'),
                'page_size': context['page'].paginator.num_pages,
                'count': context['page'].paginator.count
            })
        return JsonResponse(sku_list,safe=False)


#详情页面
class DetailView(View):
    def get(self,request,sku_id):
        try:
            sku = SKU.objects.get(id = sku_id)
        except SKU.DoesNotExist:
            pass

        categories=get_categories()

        breadcrumb= get_breadcrumb(sku.category)

        goods_specs = get_goods_specs(sku)

        context = {
            'categories': categories,
            'breadcrumb': breadcrumb,
            'sku': sku,
            'specs': goods_specs,
        }

        return render(request,'detail.html',context)


from datetime import date
from apps.goods.models import GoodsVisitCount


class CategoryVisitCountView(View):
    def post(self,request,category_id):
        try:
            category= GoodsCategory.objects.get(id= category_id)
        except GoodsCategory.DoesNotExist:
            return JsonResponse({'code':400,'errmsg':'没有此分类'})
        today= date.today()
        try:
            gvc= GoodsVisitCount.objects.get(category=category,date=today)
        except GoodsVisitCount.DoesNotExist:
            GoodsVisitCount.objects.create(category=category,
                                           date=today,
                                           count=1)
        else:
            gvc.count +=1
            gvc.save()
        return JsonResponse({'code':0,'errmsg':'ok'})


